<!DOCTYPE html>
<html lang="zh" xmlns:th="http://www.thymeleaf.org" >
<head>
    <th:block th:include="include :: header('查看日报')" />
    <th:block th:include="include :: datetimepicker-css" />
    <th:block th:include="include :: summernote-css" />
</head>
<body class="white-bg">

    <div class="wrapper wrapper-content animated fadeInRight ibox-content">
        <form class="form-horizontal m" id="form-daliy-edit" th:object="${adminDaliy}">
            <input name="daliyId" th:field="*{daliyId}" type="hidden">
            <div class="form-group">
                <label class="col-sm-3 control-label">日报日期：</label>
                <div class="col-sm-8">
                    <div class="input-group date">
                        <input name="daliyDate" class="form-control" disabled="disabled" placeholder="yyyy-MM-dd" type="text" th:value="${#dates.format(adminDaliy.daliyDate, 'yyyy-MM-dd')}">
                        <span class="input-group-addon"><i class="fa fa-calendar"></i></span>
                    </div>
                </div>
            </div>
            <div id="taskContent">
                <div id="adminTask" th:each="t, stat:${adminDaliy.daliyTask}">
                    <div class="ibox float-e-margins" >
                        <div class="ibox-title">
                            <h5>工作任务</h5><input th:name="'daliyTask['+${stat.index}+'].taskId'" class="form-control" type="hidden" th:value="${t.taskId}">
                        </div>
                        <div class="ibox-content">
                            <div class="form-group">
                                <label class="col-sm-3 control-label">产品/项目：</label>
                                <div class="col-sm-8">
                                    <select th:name="'daliyTask['+${stat.index}+'].productType'" onChange="selectProduct(this)" class="form-control" disabled="disabled">
                                        <option value="">请选择</option>
                                        <option value="0" th:selected="${t.productType==0}">产品</option>
                                        <option value="1" th:selected="${t.productType==1}">项目</option>
                                    </select>
                                </div>
                            </div>
                            <div class="form-group">
                                <label class="col-sm-3 control-label">产品/项目名称：</label>
                                <div class="col-sm-8">
                                    <select th:name="'daliyTask['+${stat.index}+'].productId'" class="form-control" disabled="disabled">
                                        <option value="">请选择产品/项目</option>
                                    </select>
                                </div>
                            </div>
                            <div class="form-group">
                                <label class="col-sm-3 control-label">需求/任务编号：</label>
                                <div class="col-sm-8">
                                    <input th:name="'daliyTask['+${stat.index}+'].taskNumber'" class="form-control disabled" type="text" th:value="${t.taskNumber}" disabled="disabled">
                                </div>
                            </div>
                            <div class="form-group">
                                <label class="col-sm-3 control-label">需求来源：</label>
                                <div class="col-sm-8">
                                    <select th:name="'daliyTask['+${stat.index}+'].projectId'" class="form-control " th:with="project=${@sysProjectService.selectSysProjectList(null)}" disabled="disabled">
                                        <option value="0">本部</option>
                                        <option th:each="p : ${project}" th:text="${p.projectName}" th:value="${p.projectId}" th:selected="${t.projectId==p.projectId}"></option>
                                    </select>
                                </div>
                            </div>
                            <div class="form-group">
                                <label class="col-sm-3 control-label">实际工作时长（小时数）：</label>
                                <div class="col-sm-8">
                                    <input th:name="'daliyTask['+${stat.index}+'].workTime'" class="form-control" type="text" th:value="${t.workTime}" disabled="disabled">
                                </div>
                            </div>
                            <div class="form-group">
                                <label class="col-sm-3 control-label">标准工作时长（小时数）：</label>
                                <div class="col-sm-8">
                                    <input th:name="'daliyTask['+${stat.index}+'].standardTime'" class="form-control" type="text" th:value="${t.standardTime}" disabled="disabled">
                                </div>
                            </div>
                            <div class="form-group">
                                <label class="col-sm-3 control-label">提交代码量（行数）：</label>
                                <div class="col-sm-8">
                                    <input th:name="'daliyTask['+${stat.index}+'].codeAmount'" class="form-control" type="text" th:value="${t.codeAmount}" disabled="disabled">
                                </div>
                            </div>
                            <div class="form-group">
                                <label class="col-sm-3 control-label">工作内容：</label>
                                <div class="col-sm-8">
                                    <textarea th:name="'daliyTask['+${stat.index}+'].taskContent'" class="form-control" disabled="disabled">[[${t.taskContent}]]</textarea>
                                </div>
                            </div>
                            <div class="form-group">
                                <label class="col-sm-3 control-label">完成比例：</label>
                                <div class="col-sm-8">
                                    <select th:name="'daliyTask['+${stat.index}+'].percentage'" th:with="type=${@dict.getType('sys_percentage')}" class="form-control " disabled="disabled">
                                        <option value="100">100%</option>
                                        <option th:each="dict : ${type}" th:text="${dict.dictLabel}" th:value="${dict.dictValue}" th:selected="${t.percentage==dict.dictValue}"></option>
                                    </select>
                                </div>
                            </div>
                            <div class="form-group">
                                <label class="col-sm-3 control-label">备注：</label>
                                <div class="col-sm-8">
                                    <textarea th:name="'daliyTask['+${stat.index}+'].remark'" class="form-control" disabled="disabled">[[${t.remark}]]</textarea>
                                </div>
                            </div>
                        </div>
                    </div>
                </div>
            </div>

        </form>
    </div>

<th:block th:include="include :: footer" />
<th:block th:include="include :: datetimepicker-js" />
<th:block th:include="include :: summernote-js" />
<th:block th:include="include :: select2-css" />
<th:block th:include="include :: select2-js" />
<script th:inline="javascript">
    var num=0
    function selectProduct(sel){
        var str = sel.name;
        let index = str.indexOf(".")
        str =str .substring(0,index);
        str += ".productId";
        var url;
        var productType= sel.value;
        url = "/system/product/"+ productType;
        if ("" === productType) {
            $("select[name='"+str+"']").empty();
            $("select[name='"+str+"']").append
            ('<option value="">请选择产品/项目</option>');
            $("select[name='"+str+"']").selectpicker('refresh');
            $("select[name='"+str+"']").selectpicker('render');
            return;
        }
        $.ajax({
            cache: true,
            type: "GET",
            url: url,
            async: false,
            success: function(result) {
                data = result.data
                if (result.code == web_status.SUCCESS) {
                    $("select[name='"+str+"']").empty();
                    for(var i in data){
                        // json = eval("("+data[i]+")");
                        var productId = data[i].productId;
                        var productName =data[i].productName;
                        $("select[name='"+str+"']").append
                        ('<option value="'+productId+'">'+productName+'</option>');
                    };
                    $("select[name='"+str+"']").selectpicker('refresh');
                    $("select[name='"+str+"']").selectpicker('render');
                } else {
                    $.modal.alertError(result.msg);
                }
            }
        });
    };
    var prefix = ctx + "system/daliy"
    function addTask() {
        var url;
        url = prefix + "/addTask/"+num;
        $.ajax({
            cache: true,
            type: "GET",
            url: url,
            async: false,
            success: function(result) {
                $("#taskContent").append(result);
                num++;
            }
        });
    };
    $("#form-daliy-edit").validate({
        focusCleanup: true
    });

    function submitHandler() {
        if ($.validate.form()) {
            $.operate.save(prefix + "/edit", $('#form-daliy-edit').serialize());
        }
    }


    function selectProductType(values, index){
        var productType = values.productType;
        var str = 'daliyTask['+index+'].productId';
        var url = "/system/product/"+ productType;
        $.ajax({
            cache: true,
            type: "GET",
            url: url,
            async: false,
            success: function(result) {
                data = result.data
                if (result.code == web_status.SUCCESS) {
                    $("select[name='"+str+"']").empty();
                    for(var i in data){
                        var productId = data[i].productId;
                        var productName =data[i].productName;
                        var flg = values.productId==productId;
                        if (flg) {
                            $("select[name='"+str+"']").append
                            ('<option value="'+productId+'" selected="selected">'+productName+'</option>');
                        } else {
                            $("select[name='"+str+"']").append
                            ('<option value="'+productId+'" >'+productName+'</option>');
                        }

                    };
                    $("select[name='"+str+"']").selectpicker('refresh');
                    $("select[name='"+str+"']").selectpicker('render');
                } else {
                    $.modal.alertError(result.msg);
                }
            }
        });
    }
    $(function() {
        num = [[${adminDaliy.daliyTask.size()}]]+1;
        $("input[name='daliyDate']").datetimepicker({
            format: "yyyy-mm-dd",
            minView: "month",
            autoclose: true
        });
        // addTask(num);
        var tasks = [[${adminDaliy.daliyTask}]];
        tasks.forEach(selectProductType);
    });
    function delTask(num) {
        var dev = "#div_" + num;
        $(dev).remove();
    };
</script>
</body>
</html>